<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles" style="width: 100%">
              <!-- type:index会自动生成序号 -->
              <el-table-column label="序号" type="index" :index="indexMethod" width="50" />
              <el-table-column prop="name" label="角色名称" width="180" />
              <el-table-column prop="description" label="描述" />
              <el-table-column width="300" label="操作">
                <template slot-scope="scope">
                  <el-button size="mini" type="success" @click="hAssign(scope.row)">分配权限</el-button>
                  <el-button size="mini" @click="hEdit(scope.row)">编辑</el-button>
                  <el-button size="mini" type="danger" @click="delRoles(scope)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pageParams.page"
                :page-sizes="[1, 2, 5]"
                :page-size="pageParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <el-dialog
        :title="isEdit ?'编辑':'增加'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <el-form ref="roleForm" :model="from" label-width="100px">
          <el-form-item label="角色名称">
            <el-input v-model="from.name" type="text" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="from.description" type="text" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog=false">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit()">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限的弹层 -->
      <el-dialog
        title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialogAssign"
      >
        <assign-permission v-if="showDialogAssign" :role-id="roleId" @close="showDialogAssign=false" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoles, delRoles, addRoles, setRoles } from '@/api/setting'
import assignPermission from './assignPermission.vue'
export default {
  name: 'Settings',
  components: {
    assignPermission
  },
  props: {},
  data() {
    return {
      // 新增弹框
      showDialog: false,
      showDialogAssign: false, // 分配权限对话框
      // 弹框 表单数据
      from: {
        name: '',
        description: ''
      },
      // 判断是编辑还是新增
      isEdit: false,
      // 表格数据
      roles: [],
      // 分页数据
      pageParams: {
        page: 1, // 查询第一页
        pagesize: 5 // 每页两条  --- 要与pagination中page-size一致
      },
      total: 0, // 总的数据条数
      roleId: ''
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getRolesApi()
  },
  mounted() {},
  methods: {
    // 获取数据
    async getRolesApi() {
      try {
        const res = await getRoles(this.pageParams)
        this.roles = res.data.rows
        this.total = res.data.total
      } catch (err) {
        console.log(err)
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageParams.pagesize = val
      this.getRolesApi()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      // 1. 更新页码
      this.pageParams.page = val
      // 2. 重发请求
      this.getRolesApi()
    },
    // 删除角色
    delRoles(t) {
      if (this.roles.length === 1) {
        this.pageParams.page--
        if (this.pageParams.page < 1) {
          this.pageParams.page = 1
        }
      }
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 删除数据
        await delRoles(t.row.id)
        // 重新渲染
        await this.getRolesApi()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 表格序号
    indexMethod(index) {
      return index + 1 + (this.pageParams.page - 1) * (this.pageParams.pagesize)
    },
    async hSubmit() {
      await this.isEdit ? this.doEdit() : this.doAdd()
      // 1.渲染页面
      await this.getRolesApi()
      // 2.关闭弹窗
      this.showDialog = false
    },

    // 增加数据
    hAdd() {
      // 1.是否为增加
      this.isEdit = false
      // 2.显示弹层
      this.showDialog = true
    },
    async doAdd() {
      // 发请求添加数据
      await addRoles(this.from)
      console.log(this.from)
      // 跳转页面到最后一页
      this.total++
      this.pageParams.page = Math.ceil(this.total / this.pageParams.pagesize)
      // 重新加载
      this.getRolesApi()
      // 提示成功
      this.$message.success('添加成功')
    },
    // 修改数据
    hEdit(row) {
      // 1. 把当前的数据直接给表单
      this.from = { ...row }
      // 2. 显示弹层
      this.showDialog = true
      // 3.是否编辑
      this.isEdit = true
    },
    async doEdit() {
      await setRoles(this.from)
      // 提示成功
      this.$message.success('修改成功')
    },
    hAssign(row) {
      this.showDialogAssign = true
      this.roleId = row.id
    }
  }
}
</script>

<style scoped lang='less'>

</style>

